<template>
	<view v-if="cartList.length" style="padding-bottom:50px;">
		<!-- 地址组件 -->
		<my-address></my-address>
		<!-- 放主体内容的盒子 -->
		<view class="cart-body">
			<view class="title-box">
				<uni-icons class="shop-icon" type="shop" size="25"></uni-icons>
				<text>购物车</text>
			</view>
			<uni-swipe-action>
				<block v-for="item in cartList">
					<uni-swipe-action-item @click="doDel(item.goods_id)" :right-options="options">
						<view class="goods-item" @click="toDetail(item)">
							<view class="pic-box">
								<label class="radio">
									<radio color="#c00" @click.stop="setChecked(item)" :checked="item.goods_checked" />
								</label>
								<!-- 左边图片 -->
								<image class="pic"
									:src="item.goods_small_logo || 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'">
								</image>
							</view>
							<!-- 右边的盒子 -->
							<view class="info-box">
								<text class="title">{{ item.goods_name }}</text>
								<view class="price-box">
									<text class="price">￥ {{ item.goods_price.toFixed(2) }}</text>
									<uni-number-box @click.native.stop :value="item.goods_count"
										@change="changeValue(item, $event)" />
								</view>

							</view>
						</view>
					</uni-swipe-action-item>
				</block>
			</uni-swipe-action>
		</view>
		<my-settle></my-settle>
	</view>

	<view v-else class="empty-box">
		<image src="../../static/cart_empty@2x.png"></image>
		<text>空空如也~</text>
	</view>
</template>

<script>
	import tabbarBadge from '../../mixins/tabbar-badge.js'
	// 导入辅助工具
	import {
		mapState
	} from 'vuex'
	export default {
		mixins: [tabbarBadge],
		computed: {
			...mapState('cart', ['cartList'])
		},

		data() {
			return {
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#c00'
					}
				}],
			};
		},
		methods: {
			doDel(id) {
				// 点谁就删谁
				this.$store.commit('cart/delGoodsById', id)
			},
			// 数量改变触发的事件
			changeValue(item, num) {

				this.$store.commit('cart/setGoodsCount', {
					goods_id: item.goods_id,
					goods_count: num
				})
			},
			setChecked(item) {

				this.$store.commit('cart/setGoodsChecked', item.goods_id)
			},
			// 每个商品的点击事件
			toDetail(item) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})

			},
		}
	}
</script>

<style lang="scss">
	.empty-box {
		width: 100vw;
		height: 100vh;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;

		image {
			width: 80px;
			height: 80px;
		}
	}

	.title-box {
		padding: 10px;
		border-bottom: 1px solid #ddd;

		.shop-icon {
			margin-right: 10px;
		}

		text {
			vertical-align: middle;
		}
	}

	.goods-item {
		display: flex;
		margin: 20rpx;

		.pic-box {
			display: flex;
			align-items: center;

			.pic {
				width: 260rpx;
				height: 260rpx;
				margin-right: 10px;
			}
		}

		.info-box {
			flex: 1;
			display: flex;
			flex-direction: column;
			justify-content: space-between;

			.title {
				// 单词换行模式，意思是一行不够放，整个单词另起一行
				word-break: break-all;
			}

			.price-box {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.price {
					color: #c00;
				}
			}
		}
	}
</style>
